<?php

class DiaryTopic
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 20;

    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'getArticleList','getArticleInfo','deleteArticle','getUserArticleList'
        );
    }

    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;

        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }

    /**
     * 文章列表
     * @param $req
     * @return string
     * @throws Exception
     */
    private function getArticleList($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        $req['id'] = 0;
        if ($res) $req['id'] = $res['list'][count($res['list']) -1]['id'];
        $pageUrl = $this->pageUrl($req);
        return render('articleList', array('list' => $res['list'] , 'id' => $req['id'], 'pageUrl' => $pageUrl ));
    }

    /**
     * 文章列表
     * @param $req
     * @return string
     * @throws Exception
     */
    private function getUserArticleList($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        $req['id'] = 0;
        if ($res) $req['id'] = $res['list'][count($res['list']) -1]['id'];
        $pageUrl = $this->pageUrl($req);
        return render('articleList', array('list' => $res['list'] , 'id' => $req['id'], 'pageUrl' => $pageUrl, 'suid' => $_POST['uid'] ? $_POST['uid'] : 0 ));
    }

    /**
     * 文章详情
     * @param $req
     * @return string
     * @throws Exception
     */
    private function getArticleInfo($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
//        echo "<pre>";print_r($res);die;
        return render('articleInfo', $res);
    }

    /**
     * 删除文章
     * @param unknown $req
     * @return unknown
     */
    private function deleteArticle ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (!$res) {
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }

    /**
     * 传参过滤
     * @param unknown $req
     * @return multitype:string number NULL
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'getArticleList':
                $param = array(
                    'do'        => 'article',
                    'format'    => 'json',
                    'action'    => 'getArticleList',
                    'start'  	=> empty($req['start']) ? $this->start : intval($req['start']),
                    'length'    => empty($req['length']) ? $this->length : $req['length'],
                    'id'        => empty($req['id']) ? '' : intval($req['id']),
                    'uid'        => empty($req['uid']) ? '' : intval($req['uid']),
                );
                break;
            case 'getArticleInfo':
                $param = array(
                    'do'     	=> 'article',
                    'format' 	=> 'json',
                    'action'  	=> 'getArticleInfo',
                    'author_uid'=> intval($req['uid']),
                    'aid'       => intval($req['aid'])
                );
                break;
            case 'deleteArticle':
                $param = array(
                    'do'     	=> 'article',
                    'format' 	=> 'json',
                    'action'  	=> 'deleteArticle',
                    'author_uid'=> intval($req['uid']),
                    'aid'       => intval($req['aid'])
                );
                break;
            case 'getUserArticleList':
                $param = array(
                    'do'     	=> 'article',
                    'format' 	=> 'json',
                    'action'  	=> 'getUserArticleList',
                    'uid'       => intval($req['uid']),
                    'id'       => empty($req['id']) ? '' : intval($req['id']),


                );
                break;

        }

        return $param;
    }

    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "getArticleList":
                $mpurl .= "&id={$req['id']}";
                break;
            case "getUserArticleList":
                $mpurl .= "&id={$req['id']}";
                break;
        }
        $pageurl = multi($req['start'], $this->length, $mpurl);
        return $pageurl;
    }

}

$req = request();
$diaryTopic   = new DiaryTopic($_SC ['apiurl']);
echo $diaryTopic->execute($req['op']);

